<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Coin extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->verify_user();
	}

	public function index()
	{
		$this->view('coin');
	}

	public function data()
	{
		$page = ($this->input->post('page') ? intval($this->input->post('page')) : 1);
		$rows = ($this->input->post('rows') ? intval($this->input->post('rows')) : 10);
		$id = intval($this->input->post('id'));
		$name = ($this->input->post('name') ? mysqli_real_escape_string($this->input->post('name')) : '');
		$status = intval($this->input->post('status'));

		if ($id) {
			$this->db->where('id', $id);
		}

		if ($id) {
			$this->db->where('status', $status);
		}

		if ($name) {
			$this->db->like('cn_name', $name);
			$this->db->or_like('en_name', $name);
		}

		$result['total'] = $this->db->count_all_results('coin');
		$start = ($page - 1) * $rows;
		$this->db->limit($rows, $start);
		$this->db->select();
		$rs = $this->db->get('coin');
		$result['rows'] = $rs->result_array();
		echo json_encode($result);
	}

	public function add()
	{
		$this->load->view('coin_add');
	}

	public function insert()
	{
		$cn_name = $this->input->post('cn_name');
		$en_name = $this->input->post('en_name');
		$en_fullname = $this->input->post('en_fullname');
		$icon = $this->input->post('icon');
		$sort = $this->input->post('sort');
		$is_db = $this->input->post('is_db');
		$status = $this->input->post('status');
		$data = array('cn_name' => $cn_name, 'en_name' => $en_name, 'en_fullname' => $en_fullname, 'icon' => $icon, 'is_db' => $is_db, 'status' => $status, 'sort' => $sort);
		$rs = $this->db->insert('coin', $data);

		if ($rs) {
			echo json_encode(array('msg' => '添加成功', 'error' => 0));
		}
		else {
			echo json_encode(array('msg' => '添加失败', 'error' => 1));
		}
	}

	public function edit($id)
	{
		$id = intval($id);
		$this->db->where('id', $id);
		$rs = $this->db->get('coin');
		$d = $rs->row_array();
		$this->load->view('coin_edit', $d);
	}

	public function update()
	{
		$cn_name = $this->input->post('cn_name');
		$en_name = $this->input->post('en_name');
		$en_fullname = $this->input->post('en_fullname');
		$icon = $this->input->post('icon');
		$sort = $this->input->post('sort');
		$is_db = $this->input->post('is_db');
		$status = $this->input->post('status');
		$data = array('cn_name' => $cn_name, 'en_name' => $en_name, 'en_fullname' => $en_fullname, 'icon' => $icon, 'is_db' => $is_db, 'status' => $status, 'sort' => $sort);
		$id = intval($this->input->post('id'));
		$rs = $this->db->where('id', $id)->update('coin', $data);

		if ($rs) {
			echo json_encode(array('msg' => '修改成功', 'error' => 0));
		}
		else {
			echo json_encode(array('msg' => '修改失败', 'error' => 1));
		}
	}

	public function del()
	{
		$id = $this->input->get('id');

		if ($id) {
			$rs = $this->db->where_in('id', $id)->delete('coin');

			if ($rs) {
				echo json_encode(array('msg' => '删除成功', 'error' => 0));
			}
			else {
				echo json_encode(array('msg' => '删除失败', 'error' => 1));
			}
		}
		else {
			echo json_encode(array('msg' => '删除失败！至少选择一行数据', 'error' => 0));
		}
	}

	public function upload()
	{
		$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . '/upload/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = 5000;
		$config['file_name'] = rand(1000, 9999) . time();
		$this->load->library('upload', $config);

		if (!$this->upload->do_upload('Filedata')) {
			$data['error'] = 1;
			$data['msg'] = '上传失败';
		}
		else {
			$data['error'] = 0;
			$data['msg'] = '上传成功';
			$d = $this->upload->data();
			$data['file_name'] = $d['file_name'];
		}

		echo json_encode($data);
	}

	public function addxnb()
	{
		$id = $this->input->post('id');

		if (!$id) {
			ajax_return(1, '找不到币种');
		}

		$coin = $this->db->where('id', $id)->get('coin')->row_array();

		if (!$coin) {
			ajax_return(1, '找不到币种');
		}

		$this->db->query('set autocommit=0');
		$this->db->query('lock tables t_user write,t_xnb write');
		$rs = array();
		$users = $this->db->get('user')->result_array();

		foreach ($users as $k => $v) {
			$xnb = $this->db->where('user_id', $v['id'])->where('coin_id', $id)->get('xnb')->row_array();

			if (!$xnb) {
				$data = array('user_id' => $v['id'], 'coin_id' => $id, 'add_time' => time());
				$rs[] = $this->db->insert('xnb', $data);
			}
		}

		if (correct($rs)) {
			$this->db->query('commit');
			$this->db->query('unlock tables');
			ajax_return(0, '操作成功');
		}
		else {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '不需要更新');
		}
	}
}

?>
